package com.dmap.admin.service.rpc.impl;

import com.dmap.admin.bo.company.CompanyAPPInfo;
import com.dmap.admin.cache.CompanyInfoCacheDao;
import com.dmap.admin.dao.ProjectMapper;
import com.dmap.admin.po.App;
import com.dmap.admin.po.Company;
import com.dmap.admin.po.Project;
import com.dmap.admin.service.backgroundv2.AppService;
import com.dmap.admin.service.backgroundv2.CompanyService;
import com.dmap.admin.service.backgroundv2.ProjectService;
import com.dmap.admin.service.rpc.CompanyInfoService;
import net.sf.json.JSONArray;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 25/05/18
 * \* Description:
 * \
 */
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {

    @Resource
    private AppService appService;
    @Resource
    private ProjectService projectService;
    @Resource
    private CompanyService companyService;
    @Resource
    private CompanyInfoCacheDao companyInfoCacheDao;
    @Resource
    private ProjectMapper projectMapper;

    @Override
    public List<Company> selectByProjectId(String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return Collections.emptyList();
        }
        String value = companyInfoCacheDao.getCompanyInfoCache(projectId);
        List<Company> returnList;
        //如果redis中有数据，则直接取redis中的数据
        if (StringUtils.isNotBlank(value)) {
            JSONArray json = JSONArray.fromObject(value);//value是一个json字符串
            returnList = (List<Company>) JSONArray.toCollection(json, Company.class);
            return returnList;
        }
        App app = appService.selectByProjectId(projectId);
        if (app == null) {
            return Collections.emptyList();
        }
        List<Project> projectList = projectService.selectByAppId(app.getId());
        if (CollectionUtils.isEmpty(projectList)) {
            return Collections.emptyList();
        }
        List<Long> companyIdList = new ArrayList<>();
        for (Project project : projectList) {
            if (project != null && project.getCompanyId() != null
                    && !project.getCompanyId().equals(0L)) {
                companyIdList.add(project.getCompanyId());
            }
        }
        if (CollectionUtils.isEmpty(companyIdList)) {
            return Collections.emptyList();
        }
        //查出公司信息之后，将其存到redis中
        returnList = companyService.selectByIdList(companyIdList);
        value = JSONArray.fromObject(returnList).toString();
        companyInfoCacheDao.addCompanyInfoCache(projectId, value);
        return returnList;
    }

    public CompanyAPPInfo selectCompanyAPPInfo(String appNumber) {
        CompanyAPPInfo companyAPPInfo = new CompanyAPPInfo();
        App app = appService.selectByProjectId(appNumber);
        if (app != null) {
            companyAPPInfo.setAppId(app.getId());
            companyAPPInfo.setAppNumber(app.getProjectId());
            companyAPPInfo.setAppName(app.getProjectName());
            List<Project> projectList = projectService.selectByAppId(app.getId());
            if (CollectionUtils.isNotEmpty(projectList)) {
                Project project = projectList.get(0);
                Company company = companyService.selectById(project.getCompanyId());
                if (company != null) {
                    companyAPPInfo.setCompanyId(company.getId());
                    companyAPPInfo.setCompanyName(company.getName());
                    companyAPPInfo.setCompanyNumber(company.getNumber());
                }
            }
        }
        return companyAPPInfo;
    }


    @Override
    public List<String> selectByCompanyId(Long companyId) {
        String value = companyInfoCacheDao.getCompanyProjectIdCache(companyId.toString());
        List<String> returnList;
        //如果redis中有数据，则直接取redis中的数据
        if (StringUtils.isNotBlank(value)) {
            JSONArray json = JSONArray.fromObject(value);//value是一个json字符串
            returnList = (List<String>) JSONArray.toCollection(json, String.class);
            return returnList;
        }
        returnList = projectMapper.selectByCompanyId(companyId);
        //添加缓存
        value = JSONArray.fromObject(returnList).toString();
        companyInfoCacheDao.addCompanyInfoCache(companyId.toString(), value);
        return returnList;
    }

}